﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Data.Common;
using System.Security.Cryptography;

namespace MsgSender.models
{
    /// <summary>
    /// 用户实体
    /// </summary>
    public class UserModel : BaseModel<UserModel>
    {
        /// <summary>
        /// ID 名称的MD5值
        /// </summary>
        public string ID { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// 发送记录索引
        /// </summary>
        public int SendIndex { get; set; }

        /// <summary>
        /// 最后发送时间
        /// </summary>
        public DateTime LastSendTime { get; set; }

        public override bool Add()
        {
            try
            {
                SQLiteCommand cmd = new SQLiteCommand();
                this.ID = EnDecrypt.EnDecrypt.MD5Encrypt(this.Name);
                cmd.CommandText = @"insert into user( id,name,send_index,last_sendtime) values(@ID,@Name,@SendIndex,@LastSendTime)";
                cmd.Parameters.AddRange(new[] {//添加参数  
                           new SQLiteParameter("@ID",ID),  
                           new SQLiteParameter("@Name", Name),  
                           new SQLiteParameter("@SendIndex", SendIndex),
                           new SQLiteParameter("@LastSendTime", LastSendTime)
                       });
                return DBHelper.ExecuteNonQuery(cmd) > 0;
            }
            catch (Exception e) {
                _logModel.Error(e);
                return false;
            }
        }

        public override bool  Delete()
        {
            try
            {
                if (string.IsNullOrEmpty(ID)) GetIdByName();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = @"delete from user where id=@Id";
                cmd.Parameters.AddRange(new[] {//添加参数   
                           new SQLiteParameter("@ID",ID)
                       });
                return DBHelper.ExecuteNonQuery(cmd) > 0;
            }
            catch (Exception e) {
                _logModel.Error(e);
                return false;
            }
        }

        public override bool Update()
        {
            try
            {
                if (string.IsNullOrEmpty(ID)) GetIdByName();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = @"update user set name=@Name,send_index=@SendIndex,last_sendtime=@LastSendTime where id=@Id";
                cmd.Parameters.AddRange(new[] {//添加参数   
                           new SQLiteParameter("@Name", Name),  
                           new SQLiteParameter("@SendIndex", SendIndex),
                           new SQLiteParameter("@LastSendTime", LastSendTime),
                           new SQLiteParameter("@ID",ID)
                       });
                return DBHelper.ExecuteNonQuery(cmd) > 0;
            }
            catch (Exception e)
            {
                _logModel.Error(e);
                return false;
            }
        }

        public override UserModel Get()
        {
            try
            {
                UserModel model = null;
                SQLiteCommand cmd = new SQLiteCommand();
                if (string.IsNullOrEmpty(ID)) GetIdByName();
                cmd.CommandText = @"select id,name,send_index,last_sendtime from user where id=@Id";
                cmd.Parameters.AddRange(new[] {//添加参数   
                           new SQLiteParameter("@ID",ID)
                       });
                IDataReader reader = DBHelper.ExecuteReader(cmd);
                while (reader.Read())
                {
                    model = new UserModel();
                    model.ID = reader["id"].ToString();
                    model.Name = reader["name"].ToString();
                    model.SendIndex = int.Parse(reader["send_index"].ToString());
                    model.LastSendTime = DateTime.Parse(reader["last_sendtime"].ToString());
                    //这边一定要关闭掉
                    reader.Close();
                    return model;
                }
                
                return model;
            }
            catch (Exception e)
            {
                _logModel.Error(e);
                return null;
            }
        }

        /// <summary>
        /// 通过Name获取MD5值作为ID
        /// </summary>
        /// <returns></returns>
        public string GetIdByName() 
        {
            this.ID = EnDecrypt.EnDecrypt.MD5Encrypt(this.Name);
            return this.ID;
        }
    }
}
